const mongoose = require('mongoose');
module.exports = function(app){
    app.get('/', async function(req, res, next) {
        res.json({code:200,msg:'welcome!'});
    });

    app.use('/admin/admin', require('./admin/admin'));
    app.use('/admin/dictionary', require('./admin/dictionary'));
    app.use('/admin/worker', require('./admin/worker'));
    app.use('/wechat', require('./wechat'));
    app.use('/mobile', require('./mobile/mobile'));
    app.use('/user/*', async function(req,res,next){
        const WeChatUserModel = mongoose.models['WeChatUser'];
        try{
            let _id=!!req.query._id?req.query._id:req.body._id;
            let token=!!req.query.token?req.query.token:req.body.token;
            let weChatUser=await WeChatUserModel.findOne({_id:_id,token:token});
            if(!weChatUser){
                next('用户身份验证失败');
            }else{
                next();
            }
        }catch(e){
            next(e.message);
        }
    });
    app.use('/user/dictionary', require('./user/dictionary'));
    app.use('/user/worker', require('./user/worker'));
    app.use('/user/company', require('./user/company'));
    app.use('/public/worker', require('./public/worker'));
};